Unity中实现无限滑动的ScrollView

您所在的位置:网站首页 unity 滑动列表选中放大 Unity中实现无限滑动的ScrollView

Unity中实现无限滑动的ScrollView

2023-09-13 19:50| 来源: 网络整理| 查看: 265

一:效果演示

二:前言

当邮件中有1000封邮件,商店列表中有1000个物体,如果直接实例化1000条数据显示则会大大增加DrawCall,而大量不可见的数据被Mask组件排除在可视范围之外,但他们依然存在,这时就需要考虑通过一个无限滑动的ScrollView来优化渲染性能

三:实现思路

通过头下标和尾下标记录当前实例化数据的最大最小索引,之后用Content的锚点位置与当头下标的锚点位置进行比较判断滑动的方向以及是否超出滑动范围,如果正方向滑动超出范围则将第一个元素移动到最后一个,如果反方向滑动超出范围则将最后一个元素移动到第一个,这样场景中始终存在5个实例化的元素,依次改变元素的位置和显示即可

四:使用说明

——此功能脚本是对ScrollRect的扩展,所以必须添加UGUI提供的基础Scroll View——Content上必须添加GridLayoutGroup组件,通过GridLayoutGroup组件设计布局,(我在代码中对startCorner、startAxis、childAlignment和constraintCount进行了限制,不需要对其设置)——不能添加Content Size Fitter组件——先调用SetTotalCount方法设置总的数据数量再调用Init方法进行初始化 ——根据需求修改SetShow方法体 ——只适用于单向滑动的情况,不能满足竖直和水平同时滑动的需求,因为大多数无限滑动列表的使用场景都是单向的

五:完整代码

将InfiniteScrollView脚本挂载到ScrollView上

using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using System.Linq; /// /// 无限滑动列表 /// public class InfiniteScrollView : MonoBehaviour { private ScrollRect scrollRect;//滑动框组件 private RectTransform content;//滑动框的Content private GridLayoutGroup layout;//布局组件 [Header("滑动类型")] public ScrollType scrollType; [Header("固定的Item数量")] public int fixedCount; [Header("Item的预制体")] public GameObject itemPrefab; private int totalCount;//总的数据数量 private List dataList = new List();//数据实体列表 private int headIndex;//头下标 private int tailIndex;//尾下标 private Vector2 firstItemAnchoredPos;//第一个Item的锚点坐标 #region Init /// /// 实例化Item /// private void InitItem() { for (int i = 0; i < fixedCount; i++) { GameObject tempItem = Instantiate(itemPrefab, content); dataList.Add(tempItem.GetComponent()); SetShow(tempItem.GetComponent(), i); } } /// /// 设置Content大小 /// private void SetContentSize() { content.sizeDelta = new Vector2 ( layout.padding.left + layout.padding.right + totalCount * (layout.cellSize.x + layout.spacing.x) - layout.spacing.x - content.rect.width, layout.padding.top + layout.padding.bottom + totalCount * (layout.cellSize.y + layout.spacing.y) - layout.spacing.y ); } /// /// 设置布局 /// private void SetLayout() { layout.startCorner = GridLayoutGroup.Corner.UpperLeft; layout.startAxis = GridLayoutGroup.Axis.Horizontal; layout.childAlignment = TextAnchor.UpperLeft; layout.constraintCount = 1; if (scrollType == ScrollType.Horizontal) { scrollRect.horizontal = true; scrollRect.vertical = false; layout.constraint = GridLayoutGroup.Constraint.FixedRowCount; } else if (scrollType == ScrollType.Vertical) { scrollRect.horizontal = false; scrollRect.vertical = true; layout.constraint = GridLayoutGroup.Constraint.FixedColumnCount; } } /// /// 得到第一个数据的锚点位置 /// private void GetFirstItemAnchoredPos() { firstItemAnchoredPos = new Vector2 ( layout.padding.left + layout.cellSize.x / 2, -layout.padding.top - layout.cellSize.y / 2 ); } #endregion #region Main /// /// 滑动中 /// private void OnScroll(Vector2 v) { if (dataList.Count == 0) { Debug.LogWarning("先调用SetTotalCount方法设置数据总数量再调用Init方法进行初始化"); return; } if (scrollType == ScrollType.Vertical) { //向上滑 while (content.anchoredPosition.y >= layout.padding.top + (headIndex + 1) * (layout.cellSize.y + layout.spacing.y) && tailIndex != totalCount - 1) { //将数据列表中的第一个元素移动到最后一个 RectTransform item = dataList[0]; dataList.Remove(item); dataList.Add(item); //设置位置 SetPos(item, tailIndex + 1); //设置显示 SetShow(item, tailIndex + 1); headIndex++; tailIndex++; } //向下滑 while (content.anchoredPosition.y OnScroll(v)); //设置布局 SetLayout(); //设置头下标和尾下标 headIndex = 0; tailIndex = fixedCount - 1; //设置Content大小 SetContentSize(); //实例化Item InitItem(); //得到第一个Item的锚点位置 GetFirstItemAnchoredPos(); } /// /// 设置显示 /// public void SetShow(RectTransform trans, int index) { //=====根据需求进行编写 trans.GetComponentInChildren().text = index.ToString(); trans.name = index.ToString(); } /// /// 设置总的数据数量 /// public void SetTotalCount(int count) { totalCount = count; } /// /// 销毁所有的元素 /// public void DestoryAll() { for (int i = dataList.Count - 1; i >= 0; i--) { DestroyImmediate(dataList[i].gameObject); } dataList.Clear(); } #endregion } /// /// 滑动类型 /// public enum ScrollType { Horizontal,//竖直滑动 Vertical,//水平滑动 }

 



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3